package com.rmondjone.locktableview;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rmondjone.xrecyclerview.ProgressStyle;
import com.rmondjone.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 说明 可锁定首行和首列的表格视图
 * 作者 资划所
 * 创建时间 2023/6/17.
 */
public class LockTableView {
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 表格父视图
     */
    private ViewGroup mParentView;
    /**
     * 表格数据，每一行为一条数据，从表头计算
     */
    private ArrayList<ArrayList<String>> mTableDatas = new ArrayList<ArrayList<String>>();
    private ArrayList<String> sumRowDatas;
    /**
     * 表格视图
     */
    private View mTableView;
    /**
     * 是否锁定首行
     */
    private boolean isLockFirstRow = true;
    /**
     * 是否锁定首列
     */
    private boolean isLockFirstColumn = true;



    private int mDividerColor;
    private boolean dividerVisiable;
    /**
     * 最大列宽(dp)
     */
//    private int maxColumnWidth;
    private int columnWidth;

    private int rowHeight;
    /**
     * 第一行背景颜色
     */
    private int mFirstRowBackGroundColor;
    /**
     * 数据为空时的缺省值
     */
    private String mNullableString;
    /**
     * 单元格字体大小
     */
    private int mTextViewSize;
    /**
     * 表格头部字体颜色
     */
    private int mTableHeadTextColor;
    /**
     * 表格内容字体颜色
     */
    private int mTableContentTextColor;
    /**
     * 表格横向滚动监听事件
     */
    private OnTableViewListener mTableViewListener;

    /**
     * 表格横向滚动到边界监听事件
     */
    private OnTableViewRangeListener mTableViewRangeListener;

    /**
     * 表格上拉刷新、下拉加载监听事件
     */
    private OnLoadingListener mOnLoadingListener;
    /**
     * Item点击事件
     */
    private OnItemClickListenter mOnItemClickListenter;
    /**
     * Item长按事件
     */
    private OnItemLongClickListenter mOnItemLongClickListenter;
    /**
     * Item选中样式
     */
    private int mOnItemSeletor;
    /**
     * 单元格内边距
     */
    private int mCellPadding;
    /**
     * 要改变的列集合
     */
    private HashMap<Integer, Integer> mChangeColumns = new HashMap<>();


    //表格数据
    /**
     * 表格第一行数据,不包括第一个元素
     */
    private ArrayList<String> mTableFirstRowData = new ArrayList<>();
    /**
     * 表格第一列数据，不包括第一个元素
     */
    private ArrayList<String> mTableFirstColumnDatas = new ArrayList<>();


    /**
     * 表格左上角数据
     */
    private String mFirstRowFirstColumnTitle;
    /**
     * 表格每一行数据，不包括第一行和第一列
     */
    private ArrayList<ArrayList<String>> mTableRowDatas = new ArrayList<ArrayList<String>>();

    /**
     * 把所有的滚动视图放图列表，后面实现联动效果
     */
    private ArrayList<HorizontalScrollView> mScrollViews = new ArrayList<HorizontalScrollView>();


    //表格视图
    /**
     * 表格左上角视图
     */
    private TextView mColumnTitleView;
    private View lockHeadView_Text_divider;
    /**
     * 第一行布局（锁状态）
     */
    private LinearLayout mLockHeadView;
    /**
     * 第一行布局（未锁状态）
     */
    private LinearLayout mUnLockHeadView;
    /**
     * 第一行滚动视图（锁状态）
     */
    private CustomHorizontalScrollView mLockScrollView;
    /**
     * 第一行滚动视图（未锁状态）
     */
    private CustomHorizontalScrollView mUnLockScrollView;
    /**
     * 表格主视图
     */
    private XRecyclerView mTableScrollView;
    /**
     * 列表适配器
     */
    private TableViewAdapter mTableViewAdapter;

    ////////////////////
    private View v_divider_sum;
    private TextView lockFooterView_Text;
    private View lockFooterView_Text_divider;
    /**
     * 第一行布局（锁状态）
     */
    private LinearLayout lockFooterView;
    /**
     * 第一行布局（未锁状态）
     */
    private LinearLayout unLockFooterView;
    /**
     * 第一行滚动视图（锁状态）
     */
    private CustomHorizontalScrollView lockFooterView_ScrollView;
    /**
     * 第一行滚动视图（未锁状态）
     */
    private CustomHorizontalScrollView unlockFooterView_ScrollView;
    //////////////////////////////////


    /**
     * 构造方法
     *
     * @param mContext     上下文
     * @param mParentView 表格父视图
     * @param mTableDatas  表格数据
     */
    public LockTableView(Context mContext, ViewGroup mParentView,
                         ArrayList<ArrayList<String>> mTableDatas,ArrayList<String> sumRowDatas) {
        this.mContext = mContext;
        this.mParentView = mParentView;
        this.mTableDatas = mTableDatas;
        this.sumRowDatas = sumRowDatas;
        initAttrs();
    }

    /**
     * 初始化属性
     */
    private void initAttrs() {
        mTableView = LayoutInflater.from(mContext).inflate(R.layout.locktableview, null);
//        maxColumnWidth = 100;
//        minColumnWidth = 70;
//        minRowHeight = 20;
//        maxRowHeight = 60;
        columnWidth = 100;
        rowHeight = 40;
        mNullableString = "N/A";
        mTableHeadTextColor = R.color.beijin;
        mTableContentTextColor = R.color.border_color;
        mFirstRowBackGroundColor = R.color.table_head;
        mTextViewSize = 16;
        mCellPadding = DisplayUtil.dip2px(mContext, 5);
    }

    /**
     * 展现视图
     */
    public void show() {
        initData();
        initView();
        mParentView.removeAllViews();//清空视图
        mParentView.addView(mTableView);
    }


    /**
     * 初始化表格数据
     */
    private void initData() {
        if (mTableDatas != null && mTableDatas.size() > 0) {
            if (isLockFirstRow) {
                ArrayList<String> fristRowDatas = (ArrayList<String>) mTableDatas.get(0).clone();
                if (isLockFirstColumn) {
                    //锁定第一列
                    mFirstRowFirstColumnTitle = fristRowDatas.get(0);
                    fristRowDatas.remove(0);
                    mTableFirstRowData.addAll(fristRowDatas);
                    //构造第一列数据,并且构造表格每行数据
                    for (int i = 1; i < mTableDatas.size(); i++) {
                        ArrayList<String> rowDatas = (ArrayList<String>) mTableDatas.get(i).clone();
                        mTableFirstColumnDatas.add(rowDatas.get(0));
                        rowDatas.remove(0);
                        mTableRowDatas.add(rowDatas);
                    }
                } else {
                    mTableFirstRowData.addAll(fristRowDatas);
                    for (int i = 1; i < mTableDatas.size(); i++) {
                        mTableRowDatas.add(mTableDatas.get(i));
                    }
                }
            } else {
                if (isLockFirstColumn) {
                    //锁定第一列
                    //构造第一列数据,并且构造表格每行数据
                    for (int i = 0; i < mTableDatas.size(); i++) {
                        ArrayList<String> rowDatas = (ArrayList<String>) mTableDatas.get(i).clone();
                        mTableFirstColumnDatas.add(rowDatas.get(0));
                        rowDatas.remove(0);
                        mTableRowDatas.add(rowDatas);
                    }
                } else {
                    for (int i = 0; i < mTableDatas.size(); i++) {
                        mTableRowDatas.add(mTableDatas.get(i));
                    }
                }
            }
        } else {
            Toast.makeText(mContext, "表格数据为空！", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 初始化表格视图
     */
    private void initView() {
        mColumnTitleView = (TextView) mTableView.findViewById(R.id.lockHeadView_Text);
        lockHeadView_Text_divider =   mTableView.findViewById(R.id.lockHeadView_Text_divider);
        mLockHeadView = (LinearLayout) mTableView.findViewById(R.id.lockHeadView);
        mLockScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.lockHeadView_ScrollView);
        mUnLockHeadView = (LinearLayout) mTableView.findViewById(R.id.unLockHeadView);
        mUnLockScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.unlockHeadView_ScrollView);
        ///////////////////////////
        v_divider_sum  =   mTableView.findViewById(R.id.v_divider_sum);
        lockFooterView_Text = (TextView) mTableView.findViewById(R.id.lockFooterView_Text);
        lockFooterView_Text_divider =   mTableView.findViewById(R.id.lockFooterView_Text_divider);
        lockFooterView = (LinearLayout) mTableView.findViewById(R.id.lockFooterView);
        lockFooterView_ScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.lockFooterView_ScrollView);
        unLockFooterView = (LinearLayout) mTableView.findViewById(R.id.unLockFooterView);
        unlockFooterView_ScrollView = (CustomHorizontalScrollView) mTableView.findViewById(R.id.unlockFooterView_ScrollView);
        ////////////////////
        //表格主视图
        mTableScrollView = (XRecyclerView) mTableView.findViewById(R.id.table_scrollView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mTableScrollView.setLayoutManager(layoutManager);
        mTableScrollView.setArrowImageView(R.drawable.iconfont_downgrey);
        mTableScrollView.setRefreshProgressStyle(ProgressStyle.BallRotate);
        mTableScrollView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mTableScrollView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                if (mOnLoadingListener != null) {
                    mOnLoadingListener.onRefresh(mTableScrollView, mTableDatas);
                }
            }

            @Override
            public void onLoadMore() {
                if (mOnLoadingListener != null) {
                    mOnLoadingListener.onLoadMore(mTableScrollView, mTableDatas);
                }
            }
        });
        mTableViewAdapter = new TableViewAdapter(mContext,
                mTableFirstColumnDatas,
                mTableRowDatas, isLockFirstColumn, isLockFirstRow);
        mTableViewAdapter.setCellPadding(mCellPadding);
//        mTableViewAdapter.setColumnMaxWidths(mColumnMaxWidths);
//        mTableViewAdapter.setRowMaxHeights(mRowMaxHeights);
        mTableViewAdapter.setColumnWidth(columnWidth);
        mTableViewAdapter.setRowHeight(rowHeight);
        mTableViewAdapter.setTextViewSize(mTextViewSize);
        mTableViewAdapter.setmDividerColor(mDividerColor);
        mTableViewAdapter.setDividerVisiable(dividerVisiable);
        mTableViewAdapter.setTableContentTextColor(mTableContentTextColor);
        mTableViewAdapter.setTableHeadTextColor(mTableHeadTextColor);
        mTableViewAdapter.setFristRowBackGroudColor(mFirstRowBackGroundColor);
        mTableViewAdapter.setHorizontalScrollView(new OnTableViewListener() {
            @Override
            public void onTableViewScrollChange(int x, int y) {
                changeAllScrollView(x, y);//数据
            }
        });
        if (mOnItemClickListenter != null) {
            mTableViewAdapter.setOnItemClickListenter(mOnItemClickListenter);
        }
        if (mOnItemLongClickListenter != null) {
            mTableViewAdapter.setOnItemLongClickListenter(mOnItemLongClickListenter);
        }
        if (mOnItemSeletor != 0) {
            mTableViewAdapter.setOnItemSeletor(mOnItemSeletor);
        } else {
            mTableViewAdapter.setOnItemSeletor(R.color.dashline_color);
        }
        mTableViewAdapter.setTableViewRangeListener(new OnTableViewRangeListener() {
            @Override
            public void onLeft(HorizontalScrollView view) {
                if (mTableViewRangeListener != null) {
                    mTableViewRangeListener.onLeft(view);
                }
            }

            @Override
            public void onRight(HorizontalScrollView view) {
                if (mTableViewRangeListener != null) {
                    mTableViewRangeListener.onRight(view);
                }
            }
        });
        mTableViewAdapter.setOnTableViewCreatedListener(new TableViewAdapter.OnTableViewCreatedListener() {
            @Override
            public void onTableViewCreatedCompleted(CustomHorizontalScrollView mScrollView) {
                mScrollViews.add(mScrollView);//mScrollView 当前行
            }
        });
        mTableScrollView.setAdapter(mTableViewAdapter);

        mLockHeadView.setBackgroundColor(ContextCompat.getColor(mContext, mFirstRowBackGroundColor));
        mUnLockHeadView.setBackgroundColor(ContextCompat.getColor(mContext, mFirstRowBackGroundColor));
        if (isLockFirstRow) {
            if (isLockFirstColumn) {
                mLockHeadView.setVisibility(View.VISIBLE);
                mUnLockHeadView.setVisibility(View.GONE);
            } else {
                mLockHeadView.setVisibility(View.GONE);
                mUnLockHeadView.setVisibility(View.VISIBLE);
            }
            createHeadRowView();
        } else {
            mLockHeadView.setVisibility(View.GONE);
            mUnLockHeadView.setVisibility(View.GONE);
        }
        initFooterView();
    }

    private void initFooterView() {
        if(sumRowDatas!=null&&sumRowDatas.size()>0){
            if (isLockFirstColumn) {
                lockFooterView.setVisibility(View.VISIBLE);
                unLockFooterView.setVisibility(View.GONE);
            } else {
                lockFooterView.setVisibility(View.GONE);
                unLockFooterView.setVisibility(View.VISIBLE);
            }
            v_divider_sum.setVisibility(View.VISIBLE);
            createFooterRowView();
        }else {
            lockFooterView.setVisibility(View.GONE);
            unLockFooterView.setVisibility(View.GONE);
            v_divider_sum.setVisibility(View.GONE);
        }
    }



    /**
     * 创建头部视图
     */
    private void createHeadRowView() {
        if (isLockFirstColumn) {
            mColumnTitleView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor));
            mColumnTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize);
            mColumnTitleView.setText(mFirstRowFirstColumnTitle);
            mColumnTitleView.setGravity(Gravity.CENTER);
            JSONObject field = fieldCulumns.getJSONObject(0);
            if (field.containsKey("sort")) {
                boolean sort = field.getBoolean("sort");
                if (sort) {
                    boolean asc = field.getBoolean("asc");
                    Drawable drawable = mContext.getDrawable(!asc ? R.drawable.icon_paixu_sorted : R.drawable.icon_paixu);
                    drawable.setBounds(0, 0, 40, 40);
                    mColumnTitleView.setCompoundDrawables(null, null, drawable, null);
//                    mColumnTitleView.setCompoundDrawablePadding(5);
                    mColumnTitleView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (onColumnSortClickListenter != null) onColumnSortClickListenter.
                                    onColumnItemClick(mColumnTitleView, 0);
                        }
                    });
                }
            }
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mColumnTitleView.getLayoutParams();
            layoutParams.width = DisplayUtil.dip2px(mContext, columnWidth);
            layoutParams.height = DisplayUtil.dip2px(mContext, rowHeight);
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
            mColumnTitleView.setLayoutParams(layoutParams);
            lockHeadView_Text_divider.setBackgroundColor(ContextCompat.getColor(mContext, mDividerColor));
            lockHeadView_Text_divider.setVisibility(dividerVisiable?View.VISIBLE:View.GONE);
            //构造滚动视图
            createFirstRowScrollview(mLockScrollView, mTableFirstRowData);
            mScrollViews.add(mLockScrollView);
            mLockScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() {
                @Override
                public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) {
                    changeAllScrollView(x, y);
                }

                @Override
                public void onScrollFarLeft(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onLeft(scrollView);
                    }
                }

                @Override
                public void onScrollFarRight(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onRight(scrollView);
                    }
                }
            });
        } else {
            createFirstRowScrollview(mUnLockScrollView, mTableFirstRowData);
            mScrollViews.add(mUnLockScrollView);
            mUnLockScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() {
                @Override
                public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) {
                    changeAllScrollView(x, y);
                }

                @Override
                public void onScrollFarLeft(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onLeft(scrollView);
                    }
                }

                @Override
                public void onScrollFarRight(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onRight(scrollView);
                    }
                }
            });
        }
    }
    private void createFooterRowView() {
        if (isLockFirstColumn) {
//            lockFooterView_Text.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor));
            lockFooterView_Text.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize);
            lockFooterView_Text.setText(sumRowDatas.get(0));
            lockFooterView_Text.setTypeface(Typeface.DEFAULT_BOLD);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mColumnTitleView.getLayoutParams();
            layoutParams.width = DisplayUtil.dip2px(mContext, columnWidth);
            layoutParams.height = DisplayUtil.dip2px(mContext, rowHeight);
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
            lockFooterView_Text.setLayoutParams(layoutParams);
            lockFooterView_Text_divider.setBackgroundColor(ContextCompat.getColor(mContext, mDividerColor));
            lockFooterView_Text_divider.setVisibility(dividerVisiable?View.VISIBLE:View.GONE);
            //构造滚动视图
            createFooterRowScrollview(lockFooterView_ScrollView, sumRowDatas.subList(1,sumRowDatas.size()));
            mScrollViews.add(lockFooterView_ScrollView);
            lockFooterView_ScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() {
                @Override
                public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) {
                    changeAllScrollView(x, y);
                }

                @Override
                public void onScrollFarLeft(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onLeft(scrollView);
                    }
                }

                @Override
                public void onScrollFarRight(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onRight(scrollView);
                    }
                }
            });
        } else {
            createFooterRowScrollview(unlockFooterView_ScrollView, sumRowDatas);
            mScrollViews.add(unlockFooterView_ScrollView);
            unlockFooterView_ScrollView.setOnScrollChangeListener(new CustomHorizontalScrollView.onScrollChangeListener() {
                @Override
                public void onScrollChanged(HorizontalScrollView scrollView, int x, int y) {
                    changeAllScrollView(x, y);
                }

                @Override
                public void onScrollFarLeft(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onLeft(scrollView);
                    }
                }

                @Override
                public void onScrollFarRight(HorizontalScrollView scrollView) {
                    if (mTableViewRangeListener != null) {
                        mTableViewRangeListener.onRight(scrollView);
                    }
                }
            });
        }
    }

    /**
     * 改变所有滚动视图位置
     *
     * @param x
     * @param y
     */
    private void changeAllScrollView(int x, int y) {
        if (mScrollViews.size() > 0) {
            if (mTableViewListener != null) {
                mTableViewListener.onTableViewScrollChange(x, y);
            }
            for (int i = 0; i < mScrollViews.size(); i++) {
                HorizontalScrollView scrollView = mScrollViews.get(i);
                scrollView.scrollTo(x, y);
            }
        }
    }

    /**
     * 构造滚动视图
     *
     * @param scrollView
     * @param datas
     */
    private void createFirstRowScrollview(
            HorizontalScrollView scrollView, List<String> datas) {
        //设置LinearLayout
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 0; i < datas.size(); i++) {
            //构造单元格
            TextView textView = new TextView(mContext);
            textView.setTextColor(ContextCompat.getColor(mContext, mTableHeadTextColor));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize);
            textView.setGravity(Gravity.CENTER);
            textView.setText(datas.get(i));
            JSONObject field = fieldCulumns.getJSONObject(isLockFirstColumn ? i + 1 : i);
            if (field.containsKey("sort")) {
                boolean sort = field.getBoolean("sort");
                if (sort) {
                    boolean asc = field.getBoolean("asc");
                    Drawable drawable = mContext.getDrawable(!asc ? R.drawable.icon_paixu_sorted : R.drawable.icon_paixu);
                    drawable.setBounds(0, 0, 40, 40);
                    textView.setCompoundDrawables(null, null, drawable, null);
                    textView.setCompoundDrawablePadding(5);
                    int finalI = i;
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (onColumnSortClickListenter != null) onColumnSortClickListenter
                                    .onColumnItemClick(textView, isLockFirstColumn ? finalI + 1 : finalI);
                        }
                    });
                }
            }
            //设置布局
            LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            textViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
            textView.setLayoutParams(textViewParams);
            ViewGroup.LayoutParams textViewParamsCopy = textView.getLayoutParams();
            if (isLockFirstColumn) {
                textViewParamsCopy.width = DisplayUtil.dip2px(mContext, columnWidth);//mColumnMaxWidths.get(i + 1));
            } else {
                textViewParamsCopy.width = DisplayUtil.dip2px(mContext, columnWidth);//mColumnMaxWidths.get(i));
            }
            linearLayout.addView(textView);
            //画分隔线
            if (i != datas.size() - 1) {
                View splitView = new View(mContext);
                ViewGroup.LayoutParams splitViewParams = new ViewGroup.LayoutParams(
                        DisplayUtil.dip2px(mContext, 1),
                        ViewGroup.LayoutParams.MATCH_PARENT);
                splitView.setLayoutParams(splitViewParams);
                splitView.setBackgroundColor(ContextCompat.getColor(mContext, mDividerColor));//ContextCompat.getColor(mContext, R.color.white));
                splitView.setVisibility(dividerVisiable?View.VISIBLE:View.GONE);
                linearLayout.addView(splitView);
            }
        }
        scrollView.addView(linearLayout);
    }

    private void createFooterRowScrollview(
            HorizontalScrollView scrollView, List<String> datas) {
        //设置LinearLayout
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setGravity(Gravity.CENTER);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 0; i < datas.size(); i++) {
            //构造单元格
            TextView textView = new TextView(mContext);
            textView.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextViewSize);
            textView.setGravity(Gravity.CENTER);
            textView.setText(datas.get(i));
            textView.setTypeface(Typeface.DEFAULT_BOLD);
            //设置布局
            LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            textViewParams.setMargins(mCellPadding, mCellPadding, mCellPadding, mCellPadding);
            textView.setLayoutParams(textViewParams);
            ViewGroup.LayoutParams textViewParamsCopy = textView.getLayoutParams();
            if (isLockFirstColumn) {
                textViewParamsCopy.width = DisplayUtil.dip2px(mContext, columnWidth);//mColumnMaxWidths.get(i + 1));
            } else {
                textViewParamsCopy.width = DisplayUtil.dip2px(mContext, columnWidth);//mColumnMaxWidths.get(i));
            }
            linearLayout.addView(textView);
            //画分隔线
            if (i != datas.size() - 1) {
                View splitView = new View(mContext);
                ViewGroup.LayoutParams splitViewParams = new ViewGroup.LayoutParams(
                        DisplayUtil.dip2px(mContext, 1),
                        ViewGroup.LayoutParams.MATCH_PARENT);
                splitView.setLayoutParams(splitViewParams);
                splitView.setBackgroundColor(ContextCompat.getColor(mContext, mDividerColor));//ContextCompat.getColor(mContext, R.color.white));
                splitView.setVisibility(dividerVisiable?View.VISIBLE:View.GONE);
                linearLayout.addView(splitView);
            }
        }
        scrollView.addView(linearLayout);
    }

//    /**
//     * 说明 改变指定列指定宽度
//     * 作者 资划所
//     * 创建时间 2018/3/15 上午11:06
//     *
//     * @param mColumnNum
//     * @param mColumnWidth
//     */
//    private void changeColumnWidth(int mColumnNum, int mColumnWidth) {
//        if (mColumnMaxWidths != null && mColumnMaxWidths.size() > 0) {
//            if (mColumnNum < mColumnMaxWidths.size() && mColumnNum >= 0) {
//                mColumnMaxWidths.set(mColumnNum, mColumnWidth + DisplayUtil.px2dip(mContext, 15) * 2);
//            } else {
//                Log.e("LockTableView", "指定列数不存在");
//            }
//        }
//    }


    //属性设置
    public LockTableView setLockFirstRow(boolean lockFirstRow) {
        isLockFirstRow = lockFirstRow;
        return this;
    }

    public LockTableView setLockFirstColumn(boolean lockFirstColumn) {
        isLockFirstColumn = lockFirstColumn;
        return this;
    }

//    public LockTableView setMaxColumnWidth(int maxColumnWidth) {
//        this.maxColumnWidth = maxColumnWidth;
//        return this;
//    }
//
//    public LockTableView setMinColumnWidth(int minColumnWidth) {
//        this.minColumnWidth = minColumnWidth;
//        return this;
//    }

    public LockTableView setFristRowBackGroudColor(int mFristRowBackGroudColor) {
        this.mFirstRowBackGroundColor = mFristRowBackGroudColor;
        return this;
    }
    public LockTableView setmDividerColor(int mDividerColor) {
        this.mDividerColor = mDividerColor;
        return this;
    }
    public LockTableView setmDividerVisiable(boolean dividerVisiable) {
        this.dividerVisiable = dividerVisiable;
        return this;
    }


    public LockTableView setNullableString(String mNullableString) {
        this.mNullableString = mNullableString;
        return this;
    }

    public LockTableView setTextViewSize(int mTextViewSize) {
        this.mTextViewSize = mTextViewSize;
        return this;
    }

    public LockTableView setTableHeadTextColor(int mTableHeadTextColor) {
        this.mTableHeadTextColor = mTableHeadTextColor;
        return this;
    }

    public LockTableView setTableContentTextColor(int mTableContentTextColor) {
        this.mTableContentTextColor = mTableContentTextColor;
        return this;
    }

//    public LockTableView setMaxRowHeight(int maxRowHeight) {
//        this.maxRowHeight = maxRowHeight;
//        return this;
//    }
//
//    public LockTableView setMinRowHeight(int minRowHeight) {
//        this.minRowHeight = minRowHeight;
//        return this;
//    }
    public LockTableView setRowHeight(int rowHeight) {
        this.rowHeight = rowHeight;
        return this;
    }

    public LockTableView setColumnWidth(int columnWidth) {
        this.columnWidth = columnWidth;
        return this;
    }

    public LockTableView setTableViewListener(OnTableViewListener mTableViewListener) {
        this.mTableViewListener = mTableViewListener;
        return this;
    }

    public LockTableView setOnLoadingListener(OnLoadingListener mOnLoadingListener) {
        this.mOnLoadingListener = mOnLoadingListener;
        return this;
    }

    public LockTableView setTableViewRangeListener(OnTableViewRangeListener mTableViewRangeListener) {
        this.mTableViewRangeListener = mTableViewRangeListener;
        return this;
    }

    public LockTableView setOnItemClickListenter(OnItemClickListenter mOnItemClickListenter) {
        this.mOnItemClickListenter = mOnItemClickListenter;
        return this;
    }

    public LockTableView setOnItemLongClickListenter(OnItemLongClickListenter mOnItemLongClickListenter) {
        this.mOnItemLongClickListenter = mOnItemLongClickListenter;
        return this;
    }

    public LockTableView setOnItemSeletor(int mOnItemSeletor) {
        this.mOnItemSeletor = mOnItemSeletor;
        return this;
    }

    public LockTableView setCellPadding(int mCellPadding) {
        this.mCellPadding = DisplayUtil.dip2px(mContext, mCellPadding);
        return this;
    }

    /**
     * 指定第几列对应的宽度
     *
     * @param mColumnNum
     * @param mColumnWidth
     * @return
     */
    public LockTableView setColumnWidth(int mColumnNum, int mColumnWidth) {
        //判断是否已经设置过
        if (mChangeColumns.containsKey(mColumnNum)) {
            mChangeColumns.remove(mColumnNum);
        }
        mChangeColumns.put(mColumnNum, mColumnWidth);
        return this;
    }

//    //值获取
//    public ArrayList<Integer> getColumnMaxWidths() {
//        return mColumnMaxWidths;
//    }
//
//    public ArrayList<Integer> getRowMaxHeights() {
//        return mRowMaxHeights;
//    }

    public LinearLayout getLockHeadView() {
        return mLockHeadView;
    }

    public LinearLayout getUnLockHeadView() {
        return mUnLockHeadView;
    }

    public XRecyclerView getTableScrollView() {
        return mTableScrollView;
    }

    public ArrayList<HorizontalScrollView> getScrollViews() {
        return mScrollViews;
    }

    /**
     * 说明 数据刷新时，重新设值
     * 作者 资划所
     * 创建时间 2023/6/17 下午2:33
     *
     * @param mTableDatas
     */
    public void setTableDatas(ArrayList<ArrayList<String>> mTableDatas) {
        this.mTableDatas = mTableDatas;
        mTableFirstRowData.clear();
        mTableFirstColumnDatas.clear();
        mTableRowDatas.clear();
//        mColumnMaxWidths.clear();
//        mRowMaxHeights.clear();
        initData();
        mTableViewAdapter.notifyDataSetChanged();
    }

    private JSONArray fieldCulumns;

    public void setColumnFields(JSONArray fieldCulumns) {
        this.fieldCulumns = fieldCulumns;
    }


    /**
     * 横向滚动监听
     */
    public interface OnTableViewListener {
        /**
         * 滚动监听
         *
         * @param x
         * @param y
         */
        void onTableViewScrollChange(int x, int y);
    }


    /**
     * 横向滚动视图滑动到边界的监听
     */
    public interface OnTableViewRangeListener {

        /**
         * 说明 最左侧
         * 作者 资划所
         * 创建时间 2017/12/14 下午4:45
         *
         * @param view
         */
        void onLeft(HorizontalScrollView view);

        /**
         * 说明 最右侧
         * 作者 资划所
         * 创建时间 2017/12/14 下午4:45
         *
         * @param view
         */
        void onRight(HorizontalScrollView view);

    }

    /**
     * 上拉刷新，下拉加载
     */
    public interface OnLoadingListener {

        /**
         * 说明 下拉刷新
         * 作者 资划所
         * 创建时间 2023/6/17 下午1:54
         *
         * @param mXRecyclerView
         * @param mTableDatas
         */
        void onRefresh(XRecyclerView mXRecyclerView, ArrayList<ArrayList<String>> mTableDatas);

        /**
         * 说明 上拉加载
         * 作者 资划所
         * 创建时间 2023/6/17 下午1:55
         *
         * @param mXRecyclerView
         * @param mTableDatas
         */
        void onLoadMore(XRecyclerView mXRecyclerView, ArrayList<ArrayList<String>> mTableDatas);
    }


    /**
     * 说明 Item点击事件
     * 作者 资划所
     * 创建时间 2018/2/2 下午4:50
     */
    public interface OnItemClickListenter {

        /**
         * @param item     点击项
         * @param position 点击位置
         */
        void onItemClick(View item, int position);
    }

    /**
     * 说明 Item长按事件
     * 作者 资划所
     * 创建时间 2018/2/2 下午4:50
     */
    public interface OnItemLongClickListenter {

        /**
         * @param item     点击项
         * @param position 点击位置
         */
        void onItemLongClick(View item, int position);
    }

    public void setOnColumnSortClickListenter(OnColumnSortClickListenter onColumnSortClickListenter) {
        this.onColumnSortClickListenter = onColumnSortClickListenter;
    }

    OnColumnSortClickListenter onColumnSortClickListenter;

    public interface OnColumnSortClickListenter {
        void onColumnItemClick(View item, int position);
    }
}
